package com.softer.chengxin.traning.service.impl;


import com.softer.chengxin.common.domin.Common;
import com.softer.chengxin.common.domin.CommonResult;
import com.softer.chengxin.common.enums.NumberEnum;
import com.softer.chengxin.corp.service.impl.ManagerServiceImpl;
import com.softer.chengxin.traning.dao.TraningDetailDao;
import com.softer.chengxin.traning.dao.TraningInfoDao;
import com.softer.chengxin.traning.entity.TraningDetail;
import com.softer.chengxin.traning.service.TraningDetailService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Gong Zhiyang
 * @date 2019/1/19
 */
@Service
public class TraningDetailServiceImpl implements TraningDetailService {
    @Autowired
    private TraningDetailDao traningDetailDao;
    @Autowired
    private TraningInfoDao traningInfoDao;
    @Autowired
    private ManagerServiceImpl managerService;

    @Override
    public CommonResult addTraning(TraningDetail traningDetail) {
        int num = traningDetailDao.addTraning(traningDetail);
            return CommonResult.response(num);
    }

    @Override
    public List<TraningDetail> getAllListByTranId(String id, String state) {
        return traningDetailDao.getAllListByTranId(id, state);
    }

    @Override
    public List<TraningDetail> getAllListByTranIdAndDep(String id, String state, String name) {
        //获取该登录账号能查询的培训部门
        String department = managerService.getDepartment(name);
        return traningDetailDao.getAllListByTranIdAndDep(id, state, department);
    }

    @Override
    public TraningDetail getInfo(String id) {
        return traningDetailDao.getInfo(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult changeState(String id, String state, String recRemark, String name) {
        //若不通过则直接修改意见
        if (String.valueOf(NumberEnum.THREE.getNum()).equals(state)) {
            if (StringUtils.isBlank(recRemark)) {
                return CommonResult.response(NumberEnum.ZERO.getNum(), "请填写意见");
            }
        }
        TraningDetail traningDetail = traningDetailDao.getInfo(id);
        int num = traningDetailDao.changeState(id, state, recRemark, name);
        //查询有几个在审核中的
        int no = traningDetailDao.selectStateByTraning(traningDetail.getTraningId());
        if (null != traningDetail) {
             traningDetailDao.selectStateByTraning(traningDetail.getTraningId());
            if (no > 0) {
                //若大于0则表示该人员 存在‘审核中’教育情况
                traningInfoDao.changeState(traningDetail.getTraningId(), Common.ONE);
            } else {
                //否则为 不存在 审核中 的教育情况
                 traningInfoDao.changeState(traningDetail.getTraningId(), Common.ZERO);
            }
        }
        return CommonResult.response(num);
    }

    @Override
    public CommonResult del(String id) {
        int num = traningDetailDao.del(id);
        return CommonResult.response(num);
    }

    @Override
    public CommonResult update(TraningDetail traningDetail) {
        int num = traningDetailDao.update(traningDetail);
        return CommonResult.response(num);
    }
    /**
     * 查询培训人员的培训记录
     * 用于培训查看页面
     *
     * @param id traning_info ID
     * @return 培训记录
     */
    @Override
    public List<TraningDetail> getTrainingDetailList(int id) {
        return traningDetailDao.getTrainingDetailList(id);
    }
}
